package cn.tedu.oa_system.service.impl;

import cn.tedu.oa_system.base.json_result.ResultDataDto;
import cn.tedu.oa_system.base.json_result.ResultDto;
import cn.tedu.oa_system.base.util.SessionUtil;
import cn.tedu.oa_system.mapper.UserMapper;
import cn.tedu.oa_system.pojo.User;
import cn.tedu.oa_system.pojo.in.*;
import cn.tedu.oa_system.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.Date;
import java.util.List;

import static cn.tedu.oa_system.base.constant.DataBaseConstants.DATABASE_AFFECTED_ROW;

/**
 * @Author MW
 * @Description TODO
 * @CreateDate 2023/1/6
 */
@Service
@Slf4j
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    SessionUtil sessionUtil;

    @Override
    public ResultDataDto<User> list(PageIn pageIn) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ne(User::getUserId,-1);
        ResultDataDto<User> resultDataDto = new ResultDataDto<>();
        Page<User> page = new Page(pageIn.getCurrent(), pageIn.getSize());
        Page<User> userPage = userMapper.selectPage(page, queryWrapper);
        List<User> records = userPage.getRecords();
        resultDataDto.setDataList(records);
        resultDataDto.setTotal(userPage.getTotal());
        resultDataDto.setIsSuccess(true);
        return resultDataDto;
    }

    @Override
    public ResultDataDto<User> findLikeKey(FindLikeKeyIn likeKeyIn) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        String likeKey = likeKeyIn.getLikeKey();
        queryWrapper.like(User::getUserName,likeKey).or()
                .like(User::getDeptId,likeKey).or()
                .like(User::getRoleId,likeKey).or()
                .like(User::getUserTel,likeKey).or()
                .like(User::getSalary,likeKey).or()
                .like(User::getRealName,likeKey);
        ResultDataDto<User> resultDataDto = new ResultDataDto<>();
        Page<User> page = new Page(likeKeyIn.getCurrent(), likeKeyIn.getSize());
        Page<User> userPage = userMapper.selectPage(page, queryWrapper);
        List<User> records = userPage.getRecords();
        resultDataDto.setDataList(records);
        resultDataDto.setTotal(userPage.getTotal());
        resultDataDto.setIsSuccess(true);
        return resultDataDto;
    }

    @Override
    public ResultDto<Void> addNewUser(AddNewUserIn addNewUserIn) {
        log.info("正在尝试添加的用户信息：[{}]",addNewUserIn);
        //此处判断用户是否有添加用户权限

        //判断该用户名是否已存在
        LambdaQueryWrapper<User> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(User::getUserName,addNewUserIn.getUserName());
        Integer i = userMapper.selectCount(wrapper);
        Assert.isTrue(i==0,"该用户名已被占用");
        User user=new User();
        BeanUtils.copyProperties(addNewUserIn,user);
        user.setModifyTime(new Date());
        //设置修改人

        /*UserInfo userInfo = sessionUtil.getUserInfo();
        user.setModifyUserId(userInfo.getId());*/

        int row = userMapper.insert(user);
        Assert.isTrue(row==DATABASE_AFFECTED_ROW,"新增用户失败，请稍后重试");
        ResultDto<Void> resultDto=new ResultDto<>();
        resultDto.setIsSuccess(true);
        resultDto.setMes("添加用户成功");
        return resultDto;
    }

    @Override
    public ResultDto<Void> updateUser(UpdateUserIn updateUserIn) {
        log.info("正在尝试修改的用户信息：[{}]",updateUserIn);
        //此处判断用户是否有添加用户权限

        //判断该用户名是否已存在
        LambdaQueryWrapper<User> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(User::getUserName,updateUserIn.getUserName());
        Integer i = userMapper.selectCount(wrapper);
        Long userId = updateUserIn.getUserId();
        User user1 = userMapper.selectById(userId);
        Assert.isTrue(user1!=null&&i!=1,"该用户名已存在");
        User user=new User();
        BeanUtils.copyProperties(updateUserIn,user);
        user.setModifyTime(new Date());
        //重置密码
        if (updateUserIn.getIsReset()) {
            user.setPassword("000000");
        }
        //设置修改人
        /*UserInfo userInfo = sessionUtil.getUserInfo();
        user.setModifyUserId(userInfo.getId());*/

        int row = userMapper.updateById(user);
        Assert.isTrue(row==DATABASE_AFFECTED_ROW,"修改用户失败，请稍后重试");
        ResultDto<Void> resultDto=new ResultDto<>();
        resultDto.setIsSuccess(true);
        resultDto.setMes("修改用户成功");
        return resultDto;
    }

    @Override
    public ResultDto<Void> deleteUser(UserIn userIn) {
        Long userId = userIn.getUserId();
        int i = userMapper.deleteById(userId);
        Assert.isTrue(i==1,"删除用户失败");
        ResultDto<Void> resultDto=new ResultDto<>();
        resultDto.setIsSuccess(true);
        resultDto.setMes("删除用户成功");
        return resultDto;
    }

}
